package org.rsbot.script.randoms;

import java.awt.Point;
import java.awt.Rectangle;
import org.rsbot.script.Random;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.methods.Bank;
import org.rsbot.script.methods.Camera;
import org.rsbot.script.methods.Interfaces;
import org.rsbot.script.methods.NPCs;
import org.rsbot.script.methods.Objects;
import org.rsbot.script.util.Timer;
import org.rsbot.script.wrappers.RSComponent;
import org.rsbot.script.wrappers.RSInterface;
import org.rsbot.script.wrappers.RSNPC;
import org.rsbot.script.wrappers.RSObject;
import org.rsbot.script.wrappers.RSPlayer;

@ScriptManifest(authors={"endoskeleton"}, name="CapnArnav", version=1.0D)
public class CapnArnav extends Random
{
  private static final int[] ARNAV_CHEST = { 42337, 42338 };
  private static final int ARNAV_ID = 2308;
  private static final int EXIT_PORTAL = 11369;
  private static final int[][] INTERFACE_SOLVE_IDS = { { 7, 14, 21 }, { 5, 12, 19 }, { 6, 13, 20 }, { 8, 15, 22 } };

  private static final int[][] ARROWS = { { 2, 3 }, { 9, 10 }, { 16, 17 } };
  private static final int TALK_INTERFACE = 228;
  private static final int CHEST_INTERFACE_PARENT = 185;
  private static final int CHEST_INTERFACE_UNLOCK = 28;
  private static final int CHEST_INTERFACE_CENTER = 23;
  private int index = -1;

  public boolean activateCondition() {
    RSNPC localRSNPC = this.npcs.getNearest(new int[] { 2308 });

    if (localRSNPC != null) {
      sleep(random(1500, 1600));
      RSObject localRSObject = this.objects.getNearest(new int[] { 11369 });

      return localRSObject != null;
    }

    return false;
  }

  public void onFinish() {
    this.index = -1;
  }

  private STATE getState() {
    if (this.objects.getNearest(new int[] { ARNAV_CHEST[1] }) != null)
      return STATE.EXIT;
    if ((this.interfaces.canContinue()) || ((this.interfaces.get(228) != null) && (this.interfaces.get(228).isValid())))
      return STATE.TALK;
    if ((this.interfaces.get(185) == null) || (!this.interfaces.get(185).isValid())) {
      return STATE.OPEN_CHEST;
    }
    return STATE.SOLVE;
  }

  public int loop()
  {
    if ((this.bank.isDepositOpen()) || (this.bank.isOpen())) {
      this.bank.close();
    }
    if (!activateCondition()) {
      return -1;
    }

    if (getMyPlayer().isMoving()) {
      return random(1000, 2000);
    }

    switch (1.$SwitchMap$org$rsbot$script$randoms$CapnArnav$STATE[getState().ordinal()]) {
    case 1:
      RSObject localRSObject1 = this.objects.getNearest(new int[] { 11369 });
      if (localRSObject1 == null) break;
      if (!localRSObject1.isOnScreen()) {
        this.camera.turnToObject(localRSObject1);
      }
      if (!localRSObject1.doAction("Enter")) break;
      return random(1000, 1300);
    case 2:
      RSObject localRSObject2 = this.objects.getNearest(ARNAV_CHEST);
      if ((localRSObject2 == null) || 
        (!localRSObject2.doClick())) break;
      return random(1000, 1300);
    case 3:
      if (this.interfaces.canContinue()) {
        this.interfaces.clickContinue();
        return random(1500, 2000);
      }
      RSComponent localRSComponent1 = this.interfaces.getComponent(228, 3);
      if ((localRSComponent1 != null) && (localRSComponent1.isValid())) {
        localRSComponent1.doClick();
      }
      return random(1500, 2000);
    case 4:
      RSInterface localRSInterface = this.interfaces.get(185);
      if ((localRSInterface == null) || (!localRSInterface.isValid()))
        break;
      String str = localRSInterface.getComponent(32).getText();
      if (str.contains("Bowl"))
        this.index = 0;
      else if (str.contains("Ring"))
        this.index = 1;
      else if (str.contains("Coin"))
        this.index = 2;
      else if (str.contains("Bar")) {
        this.index = 3;
      }

      if (solved()) {
        localRSInterface.getComponent(28).doClick();
        return random(600, 900);
      }

      RSComponent localRSComponent2 = localRSInterface.getComponent(23);
      for (int i = 0; i < 3; i++) {
        int j = random(0, 100);
        if (j < 50)
          j = 0;
        else if (j >= 50) {
          j = 1;
        }
        RSComponent localRSComponent3 = localRSInterface.getComponent(INTERFACE_SOLVE_IDS[this.index][i]);
        RSComponent localRSComponent4 = localRSInterface.getComponent(ARROWS[i][j]);

        while ((localRSComponent2.isValid()) && (localRSComponent3.isValid()) && (!localRSComponent2.getArea().contains(new Point(localRSComponent3.getCenter().x + 15, localRSComponent3.getCenter().y))) && (localRSComponent4.isValid()) && (new Timer(10000L).isRunning())) {
          localRSComponent4.doClick();
          sleep(random(1000, 1200));
        }
      }

    }

    return random(500, 800);
  }

  private boolean solved() {
    if (this.index == -1) {
      return false;
    }
    RSInterface localRSInterface = this.interfaces.get(185);
    if ((localRSInterface != null) && (localRSInterface.isValid())) {
      RSComponent localRSComponent = localRSInterface.getComponent(23);

      Point localPoint1 = localRSInterface.getComponent(INTERFACE_SOLVE_IDS[this.index][0]).getCenter();
      localPoint1.setLocation(localPoint1.x + 15, localPoint1.y);
      Point localPoint2 = localRSInterface.getComponent(INTERFACE_SOLVE_IDS[this.index][1]).getCenter();
      localPoint2.setLocation(localPoint2.x + 15, localPoint1.y);
      Point localPoint3 = localRSInterface.getComponent(INTERFACE_SOLVE_IDS[this.index][2]).getCenter();
      localPoint3.setLocation(localPoint3.x + 15, localPoint1.y);
      return (localRSComponent.getArea().contains(localPoint1)) && (localRSComponent.getArea().contains(localPoint2)) && (localRSComponent.getArea().contains(localPoint3));
    }

    return false;
  }

  private static enum STATE
  {
    OPEN_CHEST, SOLVE, TALK, EXIT;
  }
}